package com.focusai.efairy.utils;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.focusai.efairy.R;
import com.focusai.efairy.business.manager.BeedManager;
import com.focusai.efairy.ui.HomeActivity;
import com.focusai.efairy.ui.activity.StartActivity;

import java.util.List;

/**
 * Created by luo_guangwen on 2017/9/1.
 */

public class PushJumpUtils {
    public static long pushReceiverTime;
    public static final int PUSH_RECEIVE_DURATION = 1 * 1000;

    public static void jumpToHomeActivityFormPush(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, HomeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void jumpToHomeActivityFormPush2(Context context, int notificationId, String content) {
//              if (isClientTopActivy(context)) {
//            Log.D("push", "融云推送消息过来了但不用通知");
//            return;
//        }
        NotificationManager nm = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
        long currentTime = System.currentTimeMillis();
        long lastTime = pushReceiverTime;
        pushReceiverTime = currentTime;
        Intent intent = new Intent();
        intent.setClass(context, StartActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
//        多条数据需要设置不同的requestCode，否则前面的intent里数据会被覆盖
        PendingIntent pIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon))
                .setSmallIcon(R.drawable.notify_small_icon)
//                .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
//                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSound(null)
//                .setVibrate(new long[]{0,250})
                .setLights(0xff00ff00, 300, 1000)
                .setTicker(content)
                .setContentTitle(context.getString(R.string.app_name))
                .setContentText(content)
                .setContentIntent(pIntent);
//      逻辑有点复杂，设置应用在前台启动振动、声音并且时，并且消息间隔时间大于1s，开启振动与声音
//        if (currentTime - lastTime > PUSH_RECEIVE_DURATION) {
//            if (isClientTopActivy(context)) {
//                if (!UserPrefs.getNotifyVoice() && !UserPrefs.getNotifyVibration()) {
//                    builder.setVibrate(new long[]{0, 0});
//                    builder.setSound(null);
//                } else if (!UserPrefs.getNotifyVibration()) {
//                    builder.setVibrate(new long[]{0, 0});
//                    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
//                } else if (!UserPrefs.getNotifyVoice()) {
//                    builder.setSound(null);
//                    builder.setVibrate(new long[]{0, 250});
//                }
//
//            }
//        }else {
//            builder.setVibrate(new long[]{0, 0});
//            builder.setSound(null);
//        }
        Notification notif = builder.build();
        notif.flags = Notification.FLAG_AUTO_CANCEL;
        nm.notify(notificationId, notif);
        BeedManager.startpalyBeed();
    }

    public static boolean isClientTopActivy(Context context) {
        try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningTaskInfo> runningTaskInfos = activityManager.getRunningTasks(1);
            if (null != runningTaskInfos) {
                if ((runningTaskInfos.get(0).topActivity).getPackageName().startsWith(context.getPackageName())) {
                    return true;
                }

            }
        } catch (Exception ex) {
        }
        return false;
    }
}
